package com.qingxin.wuye.action;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.wuye.entity.WyPropertyCompany;
import com.qingxin.wuye.service.WyPropertyCompanyService;

@Controller
@RequestMapping("/company")
public class CompanyAction extends BaseAction {

	@Autowired
	private WyPropertyCompanyService companyService;
	
	
	
	@RequestMapping(value="/getCompanyData.json" , method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object getCompanyData(HttpServletRequest request){
		return companyService.getCompanyData(getLoginUser(request), false);
	}
	
	
	/**
	 * 列表
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/list.do", method=RequestMethod.GET)
	public String list(ModelMap model, HttpServletRequest request){
		return "wuye/company/company_list";
	}
	
	
	/**
	 * 列表数据
	 * @param pageParam
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listdata.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdata(PageParam pageParam, HttpServletRequest request){
		LoginUser loginUser = getLoginUser(request);
		return companyService.list(pageParam, loginUser, request);
	}
	
	
	/**
	 * 添加企业信息
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/add.do" , method=RequestMethod.GET)
	public String add(ModelMap model, HttpServletRequest request){
		return "wuye/company/company_add";
	}
	
	
	/**
	 * 保存企业信息
	 * @param company
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save.json", method={RequestMethod.POST})
	@ResponseBody
	public Object save(@Valid WyPropertyCompany company, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		SysError info = companyService.add(company, getLoginUser(request), request);
		return info;
	}
	
	
	/**
	 * 修改企业信息
	 * @param model
	 * @param companyid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/edit.do" , method=RequestMethod.GET)
	public String edit(ModelMap model, String companyid, HttpServletRequest request){
		WyPropertyCompany company = companyService.get(companyid);
		model.put("company", company);
		return "wuye/company/company_edit";
	}
	
	
	/**
	 * 保存修改的信息
	 * @param company
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/update.json", method={RequestMethod.POST})
	@ResponseBody
	public Object update(@Valid WyPropertyCompany company, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		SysError info = companyService.update(company, getLoginUser(request), request);
		return info;
	}
	
	
	
	/**
	 * 删除企业信息
	 * @param materialid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete.json", method={RequestMethod.POST})
	@ResponseBody
	public Object delete(String[] companyid, HttpServletRequest request){
		if(companyid==null || companyid.length==0){
			return new SysError("没有选择任何信息.");
		}
		return companyService.delete(companyid, getLoginUser(request), request);
	}
	
	
	/**
	 * 判断企业名称是否存在
	 * @param blockName
	 * @param blockid
	 * @param parkid
	 * @return
	 */
	@RequestMapping(value="/isNameDuplicate.json" , method=RequestMethod.GET)
	@ResponseBody
	public String isNameDuplicate(String companyName, String companyid){
		boolean existed = companyService.isNameDuplicate(companyName, companyid);
		return existed ? "1":"0";
	}
	
	
	
}
